<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <style>
        body {
            background-color: #ffe6f0;
            font-family: "Microsoft YaHei", sans-serif;
        }
        .register-box {
            max-width: 420px;
            margin: auto;
            padding: 30px;
            background-color: #ffffff;
            border-radius: 10px;
            box-shadow: 0 6px 12px rgba(0,0,0,0.1);
        }
        h2 {
            text-align: center;
            margin-bottom: 25px;
            color: #d63384;
        }
        .btn-primary {
            width: 100%;
        }
    </style>
</head>
<body class="d-flex align-items-center justify-content-center min-vh-100">

    <div class="register-box">
        <h2>用户注册</h2>
        <form id="registerForm">
            <div class="mb-3">
                <label>ID号</label>
                <input type="text" name="number" class="form-control" required>
            </div>
            <div class="mb-3">
                <label>姓名</label>
                <input type="text" name="name" class="form-control" required>
            </div>
            <div class="mb-3">
                <label>性别</label>
                <select name="gender" class="form-control" required>
                    <option value="">请选择</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
            <div class="mb-3">
                <label>工作单位</label>
                <input type="text" name="worker" class="form-control" required>
            </div>
            <button type="submit" class="btn btn-primary">注册</button>
        </form>

        <div id="message" class="mt-3 text-danger text-center"></div>

        <p class="mt-4 text-center">已有账号？<a href="/login_page">点击登录</a></p>
    </div>

    <script>
        document.getElementById('registerForm').onsubmit = async function (e) {
            e.preventDefault();
            const form = new FormData(this);
            const data = Object.fromEntries(form.entries());

            const res = await fetch('/register', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(data)
            });

            const json = await res.json();

            if (res.ok) {
                alert("注册成功，请前往登录");
                window.location.href = "/"; // 自动跳转
            } else {
                document.getElementById('message').innerText = json.error;
            }
        };
    </script>
</body>
</html>
